{template header common}
<script type="text/javascript" src="{__ROOT__}statics/js/haidao.validate.js?v={HD_VERSION}"></script>
<script type="text/javascript" src="template/default/statics/js/jquery.cookie.js?v={HD_VERSION}"></script>
<?php
	$setting = cache('setting');
?>

<div class="mui-content">
	<div class="padding bg-white password-find choose-way">
		<div class="list">
			<p class="hd-h5 margin-bottom text-black">请选择验证方式：</p>
			<div class="mui-pull-left hd-radio">
				<label><input class="margin-right" value="0" checked="checked" name="verify-way" type="radio">邮箱验证</label>
			</div>
			<div class="margin-big-left mui-pull-left hd-radio">
				<label><input class="margin-right" value="1" name="verify-way" type="radio">短信验证</label>
			</div>
		</div>
		<a class="mui-btn full mui-btn-blue margin-top" id="btn-way">确认选择方式</a>
	</div>
	
	<!--邮箱验证-->
	<div class="padding bg-white password-find email-dom">
		<div class="list">
			<input  type="text" name="email"  class="input"  placeholder="请输入邮箱" />
			<span class="validform_checktip"></span>
		</div>
		<a id="send_email" class="mui-btn full mui-btn-blue margin-top">发送电子邮件</a>
	</div>

	
	<!--短信验证-->
	<div class="padding bg-white mobile-dom password-find">
		<div class="list">
			<input type="text" class="input" name="mobile" placeholder="请输入手机号码" />
			<span class="validform_checktip"></span>
		</div>
		<div class="list relative">
        	<input type="text" class="number" placeholder="请输入手机验证码" name="vcode" id="vcode" disabled="disabled"  ajaxurl="{url('ajax_register_vcode_check')}"/>
        	<span class="validform_checktip"></span>
        	<button class="mui-btn mui-btn-primary ver-code" id="send_sms">发送验证码</button>
        </div>
		<a class="mui-btn full mui-btn-blue" id="next_btn">下一步</a>
	</div>
</div>

{template footer common}
</body>
</html>
<script>

	var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; //邮箱验证
    var tel_filter = /^0?1[3|4|5|8][0-9]\d{8}$/;//手机验证
	var $email=$("input[name='email']");
	var $mobile=$("input[name='mobile']");
	
	/*仿刷新：检测是否存在cookie*/
	if($.cookie("vcode_mobile")){
		reget($.cookie("vcode_mobile"));
	}
	
	//选择验证方式
	$(".choose-way").show();
	$("#btn-way").on('tap',function(){
		var $check_way=$("input[name='verify-way']:checked ");
		if($check_way.val()=='1'){
			$(".mobile-dom").show();
		}else if($check_way.val()=='0'){
			$(".email-dom").show();
		}
		$(".choose-way").hide();
	});
	
	//邮箱验证
	$("input[name='email']").on("blur",function(){
		verify($(this));
	});
	
	$("#send_email").on('tap',function(){
		if($email.data("verify")){
			get_email_data();
		}else{
			verify($email);
			if($email.data("verify")){
				get_email_data();
			}
		}
	});
	var ajax_email=true;
	function get_email_data(){
		var data={};
		data.username=$email.val();
		if(ajax_email){
			verify_ajax(data);
		}
	}

    $(".closeIcon").on('tap',function(){
        $(".popup").hide();
    })
    $(".popup-btn").on('tap',function(){
        $(".popup").hide();
    })
    //短信验证
	$("input[name='mobile']").on("blur",function(){
		verify($(this));
	});
	
	$("#next_btn").on('tap',function(){
		if(1 || $mobile.data("verify") && vocde){
            var data={};
            data.username=$mobile.val();
            data.vcode=$('#vcode').val();
			$.ajax({
                type: 'post',
                dataType: 'json',
                url: '{url("member/public/reset_password")}',
                data:data,
                success: function (result) {
                	$.tips({content:result.message});
                    if(result.message == '验证码正确'){
                        gou(1,result.referer);
                        
                    }
                }
            });
		}
	});
	
	
	/*发送验证码*/
	$("#send_sms").on('tap',function(){
		if($.cookie("vcode_mobile")){
			return false;
		}
		if($mobile.data("verify")){
			send_sms($mobile.val());
		}else{
			verify($mobile);
			if($mobile.data("verify")){
				send_sms($mobile.val());
			}
		}
	});
	
	/**
     * 页面延时跳转
     * @param secs
     * @param surl
     */
    function gou(secs, surl) {
        if (--secs > 0) {
            settimeout("gou(" + secs + ",'" + surl + "')", 1000);
        }
        else {
            location.href = surl;
        }
    }
	
	/**
     * 发送找回密码短信验证码
     * @param mobile
     */
    var $send_sms = $("#send_sms");
    function send_sms(mobile) {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '{url("member/public/forget_password")}',
            data: {username: mobile},
            success: function (data) {
            	if(data.status==1){
                    $('#vcode').removeAttr('disabled');
                    $send_sms.attr('disabled','disabled');
            		reget(60);
            	}
            	$.tips({content:data.message});
            }
        });
    }
    
	function reget(count){
		var count = count;
		var $send_sms = $("#send_sms");
		$('#send_sms').addClass('border-gray')
		var resend = setInterval(function(){
			count--;
			if (count > 0){
				$('#send_sms').html(count+'秒后重试').removeClass('bg-main').addClass('bg-gray border-gray');
				$("input[name='vcode']").removeAttr('disabled');
				$mobile.attr('readonly',true);
				$mobile.data("data-verify","");
				$send_sms.attr('disabled','disabled');
				$.cookie("vcode_mobile", count, {path: '/', expires: (1/86400)*count});
			}else {
				clearInterval(resend);
				$mobile.data("data-verify","true")
				$mobile.removeAttr('readonly');
				$send_sms.html("重获验证码").removeClass('bg-gray border-gray').addClass('bg-main').removeAttr('disabled');
				
			}
		}, 1000);
	  $send_sms.removeClass('bg-main').addClass('bg-gray');
	}
	
	
	/*验证输入*/
	function verify(account){
		var $account=account.attr("name");
		var email_verify=email_filter.test(account.val());
		var tel_verify=tel_filter.test(account.val());
		
		if(account.val()==''){
			account.siblings('.validform_checktip').html("请输入内容");
			return false;
		}
		if(($account=="email" && email_verify) || ($account=="mobile" &&　tel_verify)){
			
			account.attr("data-verify","true");
			account.siblings('.validform_checktip').html("");
				
		}else if($account=="email" && !email_verify){
			
			account.siblings('.validform_checktip').html("请输入正确的邮箱！");
			
		}else if($account=="mobile" && !tel_verify){
			
			account.siblings('.validform_checktip').html("请输入正确的电话号码！");
		}
	}
	
    /*ajax验证*/
	function verify_ajax(data){
		$(".popup").removeClass("hide");
		$.ajax({
			type:"post",
            dataType: 'json',
			url:'{url("member/public/forget_password")}',
			data:data,
			success:function(result){
				if(result.status == 1){
                    $.tips({content:"验证邮件已发送至邮箱,请5小时内查收!"});
                    
				}else{
					$.tips({content:result.message});
				}
				setTimeout(function(){
                	ajax_email=true;
                },2000);
			}
		})
		ajax_email=false;
	}

</script>

</html>